package com.wushi.lenovo.asia5b.app_di.goods.presenter;

import com.mvp.base.model.ResultModel;
import com.mvp.common.LocalManageUtil;
import com.mvp.utils.JsonUtil;
import com.mvp.utils.https.MapHelper;
import com.wushi.lenovo.asia5b.app_di.goods.mvpview.DiffCheckActivityView;
import com.wushi.lenovo.asia5b.app_http.ApiService;
import com.wushi.lenovo.asia5b.app_http.OrderService;
import com.wushi.lenovo.asia5b.app_model.good.OrderPayPriceModel;
import com.wushi.lenovo.asia5b.app_model.good.PaymentMethodModel;
import com.wushi.lenovo.asia5b.app_model.other.UserInfo;
import com.wushi.lenovo.asia5b.app_push.PushHelper;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Function;


/**
 * Created by zhangyuncai on 2020/3/6.
 */
public class DiffCheckActivityPresenter extends OrderCheckCommonPresenter<DiffCheckActivityView> {


    @Inject
    public DiffCheckActivityPresenter(ApiService apiService, OrderService orderService) {
        super(apiService,orderService);
    }


    /**
     * todo:获取价格详细,自营商品没有国内物流费用,静态参数,动态参数,包括国际物流方式,支付方式,等等
     * todo:1.首先获取地址,有地址则继续下一步,无地址请求默认地址传回UI继续下一步
     * todo:2.获取国际物流方式,有则下一步,无则请求传回UI再下一步
     * todo:3,获取支付方式,有则下一步,无则请求传回UI再下一步
     * todo:4,请求订单价格详细,传回UI
     *
     * @param extraMap 扩展参数
     */
    public void getCharges(MapHelper extraMap) {

        Observable<ResultModel<OrderPayPriceModel>> observable = Observable.just(1)
                //检测是否存在了地址
                .flatMap(new Function<Integer, ObservableSource<Integer>>() {
                    @Override
                    public ObservableSource<Integer> apply(Integer integer) throws Exception {
                        return getAddressOb(extraMap,true);
                    }
                })
                //检测是否存在了国际物流方式
//                .flatMap(new Function<Integer, ObservableSource<Integer>>() {
//                    @Override
//                    public ObservableSource<Integer> apply(Integer integer) throws Exception {
//                        if (!extraMap.containsKey("international_delivery_method")) {
//                            return getInternationalDeliveryMethodsOb(extraMap);
//                        }
//                        return Observable.just(1);
//                    }
//                })
                //检测是否存在了支付方式
                .flatMap(new Function<Integer, ObservableSource<Integer>>() {
                    @Override
                    public ObservableSource<Integer> apply(Integer integer) throws Exception {
                        if (!extraMap.containsKey("payment_method")) {
                            return getPaymentMethodsOb(extraMap);
                        }
                        return Observable.just(1);
                    }
                })
                .flatMap(new Function<Integer, ObservableSource<ResultModel<OrderPayPriceModel>>>() {
                    @Override
                    public ObservableSource<ResultModel<OrderPayPriceModel>> apply(Integer integer) throws Exception {
                        MapHelper map = MapHelper.newHashMap();
                        map.put("member_id", UserInfo.getMemberId());
                        map.put("language_code", LocalManageUtil.getAppLanguage() + "");
                        map.put("products", getProducts3(idList));
                        map.put("source", 1);
                        map.put("country_id", UserInfo.getCountryId());
                        map.put("session_id", PushHelper.getRegistrationId() + "");
                        map.put("extra_data", JsonUtil.INSTANCE.anyToJson(extraMap) + "");
                        map.put("confirm_order", 0);
                        map.put("type", "daigou");
                        map.put("pack_order", 0);
                        return apiService.getChargesOrderPay(map);
                    }
                });
        baseSubscribe(observable, resultModel -> {
            if (resultModel.isSuccess()) {
                getView().getChargesResult(resultModel.getData());
            } else {
                getView().getChargesResult(null);
            }
        }, throwable -> {
            getView().getChargesResult(null);
        });
    }


    /**
     * 加入支付方式
     *
     * @param extraMap
     * @return
     */
    private Observable<Integer> getPaymentMethodsOb(MapHelper extraMap) {
        return orderService.getPaymentMethods(getDeliveryMap())
                .flatMap(new Function<ResultModel<PaymentMethodModel>, ObservableSource<Integer>>() {
                    @Override
                    public ObservableSource<Integer> apply(ResultModel<PaymentMethodModel> paymentMethodModelResultModel) throws Exception {
                        if (paymentMethodModelResultModel.isSuccess() &&
                                paymentMethodModelResultModel.getData() != null &&
                                paymentMethodModelResultModel.getData().getPayments() != null) {
                            List<PaymentMethodModel.PaymentsBean> payments = paymentMethodModelResultModel.getData().getPayments();
                            if (payments.size() > 0) {
                                extraMap.put("payment_method", payments.get(0).getCode());
                            }
                            //todo 传回UI
                            Observable.just(payments)
                                    .observeOn(AndroidSchedulers.mainThread())
                                    .subscribe(s -> {
                                        getView().getPaymentMethodsResult(s);
                                    }, throwable -> {
                                        throwable.printStackTrace();
                                    });
                        }
                        return Observable.just(1);
                    }
                });
    }

    private MapHelper getDeliveryMap() {
        MapHelper map = MapHelper.newHashMap();
        map.put("session_id", PushHelper.getRegistrationId());
        map.put("member_id", UserInfo.getMemberId());
        map.put("language_code", LocalManageUtil.getAppLanguage() + "");
        map.put("source", 1);
        map.put("type", "daigou");
        map.put("country_id", UserInfo.getCountryId());
        map.put("confirm_order", 0);
        map.put("products", getProductsForMethod3(idList) + "");
        map.put("extra_data", getExtra_dataForDelivery(addressModel));
        return map;
    }

}